package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsMapper;
import com.atguigu.jxc.dao.GoodsTypeDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.GoodsType;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.service.GoodsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@SuppressWarnings("all")
public class GoodsServiceImpl  extends ServiceImpl<GoodsMapper, Goods>  implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Override
    public IPage<Goods> listInventory(Page<Goods> page, String codeOrName, Integer goodsTypeId) {

        return goodsMapper.findPage(page,codeOrName,goodsTypeId);
    }

    @Override
    public IPage<Goods> list(Page<Goods> goodsPage, String goodsName, Integer goodsTypeId) {
        return goodsMapper.list(goodsPage,goodsName,goodsTypeId);

    }

    @Override
    public ServiceVO saveGoods(Goods goods) {
        if (goods.getGoodsId()==null){
            // 设置上一次采购价为当前采购价
            goods.setLastPurchasingPrice(goods.getPurchasingPrice());
            goods.setInventoryQuantity(0);
            goods.setState(0);
            goodsMapper.saveGoods(goods);
        }else{
            goodsMapper.updateGoods(goods);
        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public ServiceVO deleteGoods(Integer goodsId) {
        Goods goods = goodsMapper.findByGoodsId(goodsId);

        if (goods.getState() == 1) {

            return new ServiceVO<>(ErrorCode.STORED_ERROR_CODE, ErrorCode.STORED_ERROR_MESS);

        } else if (goods.getState() == 2) {

            return new ServiceVO<>(ErrorCode.HAS_FORM_ERROR_CODE, ErrorCode.HAS_FORM_ERROR_MESS);

        } else {


            goodsMapper.deleteGoods(goodsId);

        }

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

}

